home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group98c.txt / 000142_icon-group-sender _Thu Dec 17 16:31:35 1998.msg < prev    next >
Internet Message Format  |  2000-09-20  |  2KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id QAA26236
  4.     for icon-group-addresses; Thu, 17 Dec 1998 16:31:29 -0700 (MST)
  5. Message-Id: <199812172331.QAA26236@baskerville.CS.Arizona.EDU>
  6. Date: Thu, 17 Dec 1998 13:36:27 -0700
  7. From: Steve Wampler <swampler@gemini.edu>
  8. X-Accept-Language: en
  9. To: icon-group@optima.CS.Arizona.EDU
  10. Subject: Re: Small Icon programming problem
  11. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  12. Status: RO
  13.  
  14.  
  15. Ralph Griswold wrote:
  16. > Here's a small Icon programming problem for you to tackle:
  17. >         Write a procedure digsort(i) that returns the integer that
  18. >         results from sorting the digits of i, preserving sign.  For
  19. >         example, digsort(201) should return 12 and digsort(-1042)
  20. >         should return -124.  You may assume i is an integer.
  21. > You could aim for brevity, speed, and/or clarity.
  22. > Send solutions and comments to icon-group.  The most interesting
  23. > solutions will appear in a future issue of the Icon Analyst and
  24. > sent to icon-group as a package.
  25.  
  26. Well, here's one that is short and obscure.  I have another, similar
  27. one that avoids the int->string and string->int conversions, but it
  28. runs ~20% slower than this one.
  29.  
  30. procedure digsort(i)
  31.     local a,s
  32.  
  33.     every put(a := [], !i)              # separate the digits and sign
  34.     every (s := "", s ||:= !sort(a))    # sort and rejoin (string conversion)
  35.  
  36.     return integer(s)                   # convert back to integer
  37. end
  38.  
  39. -- 
  40. Steve Wampler (swampler@gemini.edu)
  41.